<?php
namespace app\admin\controller;
use think\facade\Db;
class User extends Base{

    public function web_user($get=0,$page=1,$search='',$search_type='',$add=0,$del=0,$edit=0){
        $pt = input("post.");
        if(count($pt)>0){
            if($add == 1){
                $pt['create_time'] = time();
                $pt['up_time'] = time();
                $pt['name'] =  trim($pt['name']);
                $pt['user'] =  trim($pt['user']);
                $pt['pass'] =  md5('anan'.md5(trim($pt['pass'])));
                $bool = Db::table('tb_user')->insert($pt);
                $this->logData('新增管理员,账号：'.$pt['user'].'昵称：'.$pt['name']);
                $this->info['msg']='新增成功';
            }
            if($del == 1){
                $bool = Db::table('tb_user')->where('id',$pt['id'])->delete();
                $this->logData('删除用户,账号：'.$pt['user']);
                $this->info['msg']='删除成功';
            }
            if($edit == 1){
                if(array_key_exists("pass",$pt)){
                    $pt['pass'] = md5('anan'.md5(trim($pt['pass'])));
                }
                $bool = Db::table('tb_user')->where('id', $pt['id'])->data($pt)->update();
                $this->logData('修改用户信息,账号：'.$pt['user']);
                $this->info['msg']='修改成功';
            }
            $this->info['code']=1;
            
            return json($this->info);
        }


        $this->assign('search',$search);
        $this->assign('search_type',$search_type);
        $search_type = $search_type ? ['type' => $search_type]:[];
        $search = $search ? [['title', 'like', '%'.$search.'%']]:[];
        $listData = Db::table('tb_user')
        ->where($search_type)
        ->where($search)
        ->order('id', 'dsc')
        ->page($page,10)
        ->select()
        ->toArray(); 
        $totalNum = Db::table('tb_user')->where($search_type)->where($search)->count();
        foreach($listData as $k=>$v){
            $listData[$k]['up_time'] = date("Y-m-d G:i",$v['up_time']);
        }
        if($get!=0){
            $this->info['code']=1;
            $this->info['msg']='成功';
            $this->info['data']=$listData;
            return json($this->info);
        }else{
            $this->assign('listData',json_encode($listData));
            $this->assign('totalNum',$totalNum);
            return $this->fetch();
        }   
    }

    public function admin_user($get=0,$page=1,$search='',$search_type='',$add=0,$del=0,$edit=0){
        $pt = input("post.");

        if(count($pt)>0){
            if($add == 1){
                $pt['user'] =  trim($pt['user']);
                $isAdmin = Db::table('tb_admin')->where('user',$pt['user'])->find();
                if(!empty($isAdmin)){
                    $this->info['code']=0;
                    $this->info['msg']='改账号已存在，请重新输入';
                }else{
                    $pt['create_time'] = time();
                    $pt['up_time'] = time();
                    $pt['name'] =  trim($pt['name']);
                    $pt['pass'] =  md5('anan'.md5(trim($pt['pass'])));
                    $bool = Db::table('tb_admin')->insert($pt);
                    $this->logData('新增管理员,账号：'.$pt['user'].'昵称：'.$pt['name']);
                    $this->info['msg']='新增成功';
                    $this->info['code']=1;
                }
            }
            if($del == 1){
                $bool = Db::table('tb_admin')->where('id',$pt['id'])->delete();
                $this->logData('删除管理员,账号：'.$pt['user']);
                $this->info['msg']='删除成功';
                $this->info['code']=1;
            }
            if($edit == 1){
                if(array_key_exists("pass",$pt)){
                    $pt['pass'] = md5('anan'.md5(trim($pt['pass'])));
                }
                $bool = Db::table('tb_admin')->where('id', $pt['id'])->data($pt)->update();
                $this->logData('修改管理员信息,昵称：'.$pt['name']);
                $this->info['msg']='修改成功';
                $this->info['code']=1;
            }
            return json($this->info);
        }


        $this->assign('search',$search);
        $this->assign('search_type',$search_type);
        $search_type = $search_type ? ['type' => $search_type]:[];
        $search = $search ? [['title', 'like', '%'.$search.'%']]:[];
        $listData = Db::table('tb_admin')
        // ->fetchSql(true)//输出mysql语句
        ->alias('a')
        ->join(['tb_admin_role'=>'b'],'a.role_id = b.id')
        ->where($search_type)
        ->where($search)
        ->where([['a.id','<>',1]])
        ->order('a.id', 'dsc')
        ->field('a.id,name,user,role_name,up_time,disable,role_id')
        ->page($page,10)
        ->select()
        ->toArray(); 
        $totalNum = Db::table('tb_admin')->where($search_type)->where($search)->count();
        foreach($listData as $k=>$v){
            $listData[$k]['up_time'] = date("Y-m-d G:i",$v['up_time']);
        }
        if($get!=0){
            $this->info['code']=1;
            $this->info['msg']='成功';
            $this->info['data']=$listData;
            return json($this->info);
        }else{
            $typeList = Db::query("select * from tb_admin_role");
            $this->assign('typeList',$typeList);
            $this->assign('listData',json_encode($listData));
            $this->assign('totalNum',$totalNum);
            return $this->fetch();
        }   
    }

    //权限列表
    public function list_role(){
        $powerList = Db::table('tb_admin_power')->order('sort', 'dsc')->select()->toArray(); 
        $listData = Db::table('tb_admin_role')->order('id', 'desc')->select()->toArray(); 
        $this->assign('powerList',json_encode($powerList));
        $this->assign('powerLists',$powerList);
        $this->assign('listData',json_encode($listData));
        return $this->fetch();
    }

    //删除角色
    public function del_role(){
        $pt = input("post.");
        $bool = Db::table('tb_admin_role')->where('id',$pt['id'])->delete();
        $this->logData('删除管理员角色,'.$pt['role_name']);
        $this->info['code']=1;
        $this->info['msg']='成功';
        return json($this->info);
    }

    //添加角色
    public function add_role(){
        $pt = input("post.");
        $pt['role_name'] =  trim($pt['role_name']);
        $bool = Db::table('tb_admin_role')->insert($pt);
        $this->logData('添加管理员角色,'.$pt['role_name']);
        $this->info['code']=1;
        $this->info['msg']='成功';
        return json($this->info);
    }

    public function power_data($add=0,$del=0){
        $pt = input("post.");
        if($add == 1){
            $pt['name'] =  trim($pt['name']);
            $pt['pid'] =  trim($pt['pid']);
            $pt['url'] =  trim($pt['url']);
            $bool = Db::table('tb_admin_power')->insert($pt);
            $this->logData('添加权限节点,'.$pt['name'].'/'.$pt['url']);
        }
        if($del == 1){
            $bool = Db::table('tb_admin_power')->where('id',$pt['id'])->delete();
            $this->logData('删除权限节点,'.$pt['name']);
        }
        $this->info['code']=1;
        $this->info['msg']='成功';
        return json($this->info);
    }

    //编辑角色权限
    public function role_power_edit(){
        $pt = input("post.");
        $bool = Db::table('tb_admin_role')->where('id', $pt['id'])->data($pt)->update();
        $this->logData('修改角色权限,角色ID:'.$pt['id']);
        $this->info['code']=1;
        $this->info['msg']='成功';
        return json($this->info);
    }

    public function password(){
        $pt = input("post.");
        if(count($pt)){
            $pass = md5('anan'.md5(trim($pt['password'])));
            $where = [
                'id'=>$pt['id'],
                'pass'=> md5('anan'.md5(trim($pt['oldPassword'])))
            ];
            $adminData = Db::table('tb_admin')->where($where)->select()->toArray();
            // dump($adminData);
            // exit;
            if(count($adminData) > 0){
                $bool = Db::table('tb_admin')->where('id', $pt['id'])->data(['pass'=>$pass])->update();
                session('adminData', null);
                $this->logData('修改系统登录密码');
                $this->info['code']=1;
                $this->info['msg']='成功';
            }else{
                $this->info['code']=0;
                $this->info['data']=$adminData;
                $this->info['msg']='修改失败,当前密码有误';
                
            }
            return json($this->info);
        }
        return $this->fetch();
    }
}
